package com.zf.yichat.api.controller.tickets;


import com.zf.yichat.api.controller.BaseController;
import com.zf.yichat.api.dto.request.PlantTreeRequest;
import com.zf.yichat.api.dto.request.TicketsRequest;
import com.zf.yichat.api.dto.resp.TicketsDto;
import com.zf.yichat.mapper.UserPrintTreeConfigMapper;
import com.zf.yichat.model.StUserTicket;
import com.zf.yichat.model.SysDict;
import com.zf.yichat.model.UserPrintTreeConfig;
import com.zf.yichat.model.UserPrintTreeTask;
import com.zf.yichat.service.PlantTreeService;
import com.zf.yichat.service.SysDictService;
import com.zf.yichat.service.TicketsService;
import com.zf.yichat.service.UserIntegralService;
import com.zf.yichat.utils.common.N3d;
import com.zf.yichat.utils.response.FsPage;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import com.zf.yichat.vo.DictKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 门票系统
 */
@RestController
@RequestMapping("tickets")
@Slf4j
public class TicketsController extends BaseController {


    @Autowired
    private TicketsService ticketsService;


    /**
     * 查询用户门票列表
     * @return
     */
    @PostMapping("queryUserTickets")
    public FsResponse queryUserTickets(@RequestBody TicketsRequest request) {

        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        Integer status = request.getStatus();
        List<StUserTicket> list = ticketsService.queryUserTickets(userId, status);
        ArrayList<TicketsDto> ticketsDtos = new ArrayList<>();
        for (StUserTicket ticket : list) {
            try {
                TicketsDto dto = new TicketsDto();
                BeanUtils.copyProperties(ticket, dto);

                Long uId = ticket.getUserId();
                String encodeUserId = N3d.encode(uId);
                dto.setUserId(encodeUserId);
                dto.setPurposeType(TicketsDto.PURPOSE_TYPE_ACTIVATION);
                ticketsDtos.add(dto);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("用户id加密错误！");
            }
        }
        return FsResponseGen.successData(ticketsDtos);
    }

    /**
     * 用户扫描官方邀请码赠送门票
     * @return
     */
    @PostMapping("officialInvitationTickets")
    public FsResponse officialInvitationTickets(@RequestBody TicketsRequest request) {

        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        String ticketCode = request.getTicketCode();
        if (null == ticketCode) {
            return FsResponseGen.failMsg("参数错误【ticketCode】");
        }
        return ticketsService.officialInvitationTickets(userId, ticketCode);
    }


    /**
     * 激活用户门票
     * @return
     */
    @PostMapping("activateUserTickets")
    public FsResponse activateUserTickets(@RequestBody TicketsRequest request) throws Exception {

        Long adminUserId = getUserId();
        if (null == adminUserId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        String ticketCode = request.getTicketCode();
        if (null == ticketCode) {
            return FsResponseGen.failMsg("参数错误【ticketCode】");
        }

        String customerUserId = request.getUserId();
        Long userId = N3d.decode(customerUserId);
        return ticketsService.activateUserTickets(adminUserId, ticketCode, userId);
    }

}
